#include "WriteMultHoldRegisterCmd.h"

WriteMultHoldRegisterCmd::WriteMultHoldRegisterCmd(QObject *parent) :
    BaseModbusCmd(parent)
{
    this->m_protocolType = ProtocolType::ModbusProtocol;
    this->m_functionCode = FunctionCode;
}

Package WriteMultHoldRegisterCmd::toPackage()
{
    Package package;
    QByteArray head,body;

    quint8  deviceAddress = m_modbusControl.deviceAddress();
    quint16 registerStartAddress = m_modbusControl.registerStartAddress();
    quint16 registerNumber = m_modbusControl.registrValueList().size();
    QList<quint16> registerValueList = m_modbusControl.registrValueList();

    head.append(DataTypeConver::intToByteArray(deviceAddress,1));
    body.append(DataTypeConver::intToByteArray(registerStartAddress,2));
    body.append(DataTypeConver::intToByteArray(registerNumber,2));
    body.append(DataTypeConver::intToByteArray(registerNumber*2,1));
    foreach (const quint16 &registerValue, registerValueList)
    {
        body.append(DataTypeConver::intToByteArray(registerValue,2));
    }

    package.setHead(head);
    package.setFunction(FunctionCode);
    package.setBody(body);

    return package;
}

void WriteMultHoldRegisterCmd::unPackage(Package package)
{
    m_cmdId = package.id();
}


